package com.fandou.airobot.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.fandou.airobot.BR;
import com.fandou.airobot.R;
import com.fandou.airobot.data.bean.HomeItem;
import com.fandou.airobot.ui.alarm.AlarmActivity;
import com.fandou.airobot.ui.alarm.HomeViewModel;
import com.fandou.airobot.ui.voiceassistant.VoiceAssistantActivity;
import com.fandou.base.ui.DataBindingConfig;
import com.fandou.base.ui.event.PageClickProxy;
import com.fandou.base.ui.fragment.BaseFragment;
import com.fandou.base.view.TitleBar;
import com.fandou.base.view.recyclerview.SpaceItemDecoration;
import com.fandou.paiti.SmartCameraCropActivity;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

public class HomeFragment extends BaseFragment {

    public static final int PHOTO_CODE = 0x001;

    private HomeViewModel homeViewModel;

    @Override
    protected void initViewModel() {
        homeViewModel = getFragmentViewModel(HomeViewModel.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        ClickProxy clickProxy = new ClickProxy();
        return new DataBindingConfig(R.layout.ai_robot_home_layout, homeViewModel)
                .addBindingParam(BR.adapter, new HomeItemAdapter(getContext(), clickProxy))
                .addBindingParam(BR.itemDecoration, new SpaceItemDecoration(getResources().getDimensionPixelSize(R.dimen.land_x32)))
                .addBindingParam(BR.click, clickProxy);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        homeViewModel.getHomeItemLiveData().observe(getViewLifecycleOwner(), homeItems -> {
            if (homeItems != null) {
                homeViewModel.items.setValue(homeItems);
            }
        });
        homeViewModel.requestHomeItem();


        TitleBar titleBar = view.findViewById(R.id.titleBar);
        //点击语音图标
        titleBar.setOnIvRightClickListener(new TitleBar.OnIvRightClickListener() {
            @Override
            public void onTitleBarIvRightClickListener() {
                VoiceAssistantActivity.start(mContext, VoiceAssistantActivity.SAY_HELLO);
            }
        });
    }


    /**
     * 进入拍题
     */
    private void toTopicSearch() {
        AndPermission.with(this)
                .runtime()
                .permission(Permission.Group.STORAGE, Permission.Group.CAMERA)
                .onGranted(permissions -> {
                    // Storage permission are allowed.
                    Intent intent = new Intent(mContext, SmartCameraCropActivity.class);
                    startActivityForResult(intent, PHOTO_CODE);
                })
                .onDenied(permissions -> {
                    showErrorToast("请先授予权限");
                })
                .start();
    }


    public class ClickProxy implements PageClickProxy {

        @Override
        public void back() {
            nav().navigateUp();
        }

        public void clickItem(HomeItem homeItem) {

            if (homeItem.getActionTo() == R.id.action_aiRobotHomeFragment_to_aiRobotAlarmListFragment) {
                AlarmActivity.start(mContext);
            } else if (homeItem.getActionTo() == R.id.action_aiRobotHomeFragment_to_aiRobotPaitiFragment) {
                toTopicSearch();
            } else {
                nav().navigate(homeItem.getActionTo());
            }
        }
    }
}
